<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>后台管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="后台管理系统">
    <meta name="description" content="">

    <link rel="shortcut icon" href="/static/img/favicon.ico">
    <link rel="stylesheet" href="/static/plugins/layui_v2/css/layui.css">
    <link rel="stylesheet" href="/static/css/global.css">
    <link rel="stylesheet" type="text/css" href="/static/css/common.css" media="all">
    <link rel="stylesheet" type="text/css" href="/static/css/personal.css" media="all">
    <link rel="stylesheet" type="text/css" href="http://at.alicdn.com/t/font_9h680jcse4620529.css">
    <script src="/static/plugins/layui_v2/layui.js"></script>
<body>
<div class="larry-grid layui-anim layui-anim-upbit larryTheme-A ">
    <div class="larry-personal">
        <div class="layui-tab">
            <blockquote class="layui-elem-quote mylog-info-tit">
                <div class="layui-inline">
                    <form class="layui-form" id="userSearchForm">
                        <div class="layui-input-inline" style="width:110px;">
                            <select name="searchTerm" >
                                <option value="code">ID</option>
                                <option value="name">方法名</option>
                            </select>
                        </div>
                        <div class="layui-input-inline" style="width:145px;">
                            <input type="text" name="searchContent" value="" placeholder="请输入关键字" class="layui-input search_input">
                        </div>
                        <a class="layui-btn userSearchList_btn" lay-submit lay-filter="userSearchFilter"><i class="layui-icon larry-icon larry-chaxun7"></i>查询</a>
                    </form>
                </div>
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-normal add_btn" > <i class="layui-icon larry-icon larry-xinzeng1"></i>新增</a>
                </div>
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-danger batchFail_btn"><i class="layui-icon larry-icon larry-huishouzhan1"></i>删除</a>
                </div>
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-warm batchPause_btn"  > <i class="layui-icon larry-icon larry-danye"></i>暂停</a>
                </div>
                <div class="layui-inline">
                    <a class="layui-btn batchResume_btn" style="background-color: #fa6fb7" > <i class="layui-icon larry-icon larry-danye"></i>恢复</a>
                </div>
            </blockquote>
            <div class="larry-separate"></div>
            <!-- 用户列表 -->
            <div class="layui-tab-item layui-show " style="padding: 10px 15px;">
                <table id="schedulerTableList"  lay-filter="schedulerTableId" ></table>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    var $;
    layui.config({
        base : "/static/js/"
    }).use(['form', 'table', 'layer','common','laytpl'], function () {
         $ =  layui.$;
                var form = layui.form,
                table = layui.table,
                layer = layui.layer,
                common = layui.common,
                laytpl = layui.laytpl;

        var loading = layer.load(0,{ shade: [0.3,'#000']});
        /**用户表格加载*/
        table.render({
            elem: '#schedulerTableList',
            url: '/scheduler/list',
            id:'schedulerTableId',
            method: 'post',
            height:'full-140',
            skin:'row',
            even:'true',
            size: 'sm',
            limit: 10,
            page: true,
            cols: [[
                {#{type:"numbers"},#}
                {type:"checkbox"},
                {#{field:'id', title: 'ID',align:'left' ,width: '21%'},#}
                {field:'name', title: '方法名字',align:'center',width: '20%'},
                {field:'next_run_time', title: '下次执行时间',align:'center',width: '12%'},
                {field:'func_ref', title: '方法位置',align:'center',width: '20%'},
                {field:'args', title: '参数',align:'center',width: '8%'},
                {field:'max_instances', title: '实例',align:'center',width: '5%'},
                {field:'misfire_grace_time', title: '容错时间',align:'center',width: '8%'},
                {field:'cron', title: 'cron',align:'center',width: '10%'},
                {field:'state', title: '状态',align:'center',width: '8%', templet: '#statusTpl'},
                {title: '操作', align:'center', width: '10%',toolbar: '#userBar'}
            ]],
            done: function (res, curr, count) {
                common.resizeGrid();
                layer.close(loading);
            }
        });

        /**查询*/
        $(".userSearchList_btn").click(function(){
            var loading = layer.load(0,{ shade: [0.3,'#000']});
            //监听提交
            form.on('submit(userSearchFilter)', function (data) {
                table.reload('stockTableId',{
                    where: {
                            key:data.field.searchTerm,
                            value:data.field.searchContent
                    },
                    height: 'full-140',
                    page: true,
                    done: function (res, curr, count) {
                        common.resizeGrid();
                        layer.close(loading);

                    }
                });
            });
        });

         /**新增任务*/
        $(".add_btn").click(function(){
            var url = "/scheduler/add";
            common.cmsLayOpen('新增任务',url,'650px','590px');
        });

        /**批量删除任务*/
        $(".batchFail_btn").click(function(){
            var checkStatus = table.checkStatus('schedulerTableId');
            if(checkStatus.data.length == 0){
                common.cmsLayErrorMsg("请选择至少1个任务!");
            }else{
                var ids = [];
                $(checkStatus.data).each(function(index, item){
                    ids.push(item.id);
                });
                var url = "/scheduler/batchDelete";
                var param = {jobs:JSON.stringify(ids)};
                common.ajaxCmsConfirm('系统提示', '确认要删除任务吗?', url, param);
            }
        });

        /**批量暂停任务*/
        $(".batchPause_btn").click(function(){
            var checkStatus = table.checkStatus('schedulerTableId');
            if(checkStatus.data.length == 0){
                common.cmsLayErrorMsg("请选择至少1个任务!");
            }else{
                var status = false;
                var ids = [];
                $(checkStatus.data).each(function(index, item) {
                    ids.push(item.id);
                    //任务已暂停
                    if (item.state == 1) {
                        status = true;
                        return false;
                    } else {
                        status = false;
                    }
                });
                if (status == true) {
                    common.cmsLayErrorMsg("当前选择的任务已暂停");
                    return false;
                }
                var url = "/scheduler/batchPause";
                var param = {jobs:JSON.stringify(ids)};
                common.ajaxCmsConfirm('系统提示', '确认要暂停任务吗?', url, param);
            }
        });

        /**批量恢复任务*/
        $(".batchResume_btn").click(function() {
            var checkStatus = table.checkStatus('schedulerTableId');
            if (checkStatus.data.length == 0) {
                common.cmsLayErrorMsg("请选择至少1个任务!");
            } else {
                var ids = [];
                $(checkStatus.data).each(function(index, item){
                    ids.push(item.id);
                });
                var url = "/scheduler/batchResume";
                var param = {jobs:JSON.stringify(ids)};
                common.ajaxCmsConfirm('系统提示', '确认要恢复任务吗?', url, param);
            }
        });

        /**监听工具条*/
        table.on('tool(schedulerTableId)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            console.log(data);
            //编辑任务
            if (layEvent === 'scheduler_edit') {
                var id = data.id;
                var url =  "/scheduler/edit/"+id;
                common.cmsLayOpen('编辑任务',url,'650px','590px');
            }
        });


    });
</script>


<!-- 运行状态tpl-->
<script type="text/html" id="statusTpl">

    {{ '{{# if(d.state == 1) { }}' }}
        <span class="label label-danger ">暂停</span>
    {{ '{{# } else if(d.state == 2) { }}' }}
        <span class="label label-success">运行中</span>
    {{ '{{# } }}' }}

</script>

<!--工具条 -->
<script type="text/html" id="userBar">
    <div class="layui-btn-group">
        <a class="layui-btn layui-btn-xs user_edit" lay-event="scheduler_edit"><i class="layui-icon larry-icon larry-bianji2"></i> 编辑</a>
    </div>
</script>

</body>
</html>